import axios from 'axios';
import { Notification } from '@arco-design/web-react';
import { logout } from '@/utils/login/index';

// 创建axios实例
const request = axios.create({
  baseURL: process.env.REACT_APP_API_URL_GZ,
  withCredentials: false,
  timeout: 10 * 2000,
});

// 请求拦截器
request.interceptors.request.use(
  (config) => {
    let token = localStorage.getItem('token');
    let tokenType = localStorage.getItem('token_type');
    config.headers.Authorization = `${tokenType} ${token}`;
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 添加响应拦截器
request.interceptors.response.use(
  function (response) {
    return response;
  },
  function (error) {
    console.log(error, 'error');
    if (error.response?.status === 401) {
      logout();
      Notification.warning({
        title: '提示',
        content: '登录已过期，请重新登录！',
      });
    }

    return error.response;
  }
);

export default request;
